import {
  getDictionary,
  addFamilyMember,
  updateFamilyArchiveStatus,
  getPersonInfoApi
} from "../../../services/filing/filing"
import {
  GETSTORAGE,
  SETSTORAGE
} from "../../../services/storage/storage"
import Message from '../../../miniprogram_npm/tdesign-miniprogram/message/index'


Page({
  data: {
    // 姓名
    nameValue: '',
    nameError: false,
    // 性别
    citys: [{
        label: '男',
        value: '男'
      },
      {
        label: '女',
        value: '女'
      },
    ],
    personageSex: '',
    sexValue: '请选择性别',
    sexError: false,
    showWarnConfirm: false,
    activeValues: [0],
    cur: {},
    name: '请选择你们的关系',
    relationLeft: '',
    relationRight: '',
    relationShipId: '',
    // 弹出框
    index: '',
    join: '',
    listState: 0, //
    dialogFlag: true,
    relationFlag: false,
    // 是否主动进来的
    btnFlag: false,
    // 数据
    list: [],
    userFamilyId: ''
  },
  // 姓名
  onNameInput(e) {
    const {
      value
    } = e.detail
    if (value == '') {
      this.setData({
        nameError: true,
        nameValue: value
      })
    } else {
      this.setData({
        nameError: false,
        nameValue: value
      })
    }
  },
  // 性别
  onPickerChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;
    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      sexValue: value,
      [`${key}Text`]: value.join(' '),
    });
  },
  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  onWithoutTitlePicker() {
    this.setData({
      city2Visible: true,
      city2Title: ''
    });
  },
  // 关系
  onWithoutRelation() {
    this.setData({
      relationVisible: true,
      relationFlag: true,
      city2Title: ''
    });
    this.dictionary()
  },
  onRelationChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value,
      label,
    } = e.detail;
    console.log('picker change:', e.detail);
    this.data.list.forEach(item => {
      if (item.value == value) {
        this.setData({
          relationShipId: item.value,
          name: item.lables,
          relationLeft: item.left,
          relationRight: item.right,
        })
      }
    })
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Text`]: value.join(' '),
    });
  },
  onRelationCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  handleConcernPopup(e) {
    const {
      item
    } = e.currentTarget.dataset;
    this.setData({
        cur: item,
        relationFlag: true,
      },
      () => {
        this.setData({
          visible: true
        });
      },
    );
  },
  onVisibleChange(e) {
    this.setData({
      visible: e.detail.visible,
    });
  },
  // 补充更多信息
  handleChange(e) {
    this.setData({
      activeValues: e.detail.value,
    });
  },
  // 确定
  handlePopup() {
    this.nonEmpty()
    if (!this.data.nameError && this.data.nameValue && !this.data.sexError && this.data.sexValue != '' && this.data.name !== "请选择你们的关系") {
      const userId = GETSTORAGE('userId')
      let obj = {
        userId: userId,
        familyId: this.data.userFamilyId,
        nickName: this.data.nameValue,
        sex: this.data.sexValue[0],
        relationShip: this.data.relationShipId,
      }
      // 调用添加家人
      addFamilyMember(obj).then(res => {
        console.log("添加家人信息", obj);
        console.log("添加家人res=>", res);
      })
      this.setData({
        showWarnConfirm: true
      });
    } else {
      Message.error({
        context: this,
        offset: [20, 32],
        duration: 2000,
        content: '输入框不能为空',
      });
      console.log(12);
    }
  },

  onChangePopop(e) {
    const {
      item
    } = e.currentTarget.dataset;
    this.setData({
        cur: item,
      },
      () => {
        this.setData({
          visible: false
        });
      },
    );
  },
  // 跳过
  skip() {
    this.update()
    this.setData({
      showWarnConfirm: false
    });
    wx.navigateTo({
      url: '/pages/filing/health-management/healt-hmanagement'
    })
  },
  continueAdd() {
    this.setData({
      showWarnConfirm: true,
    });
    if (this.data.btnFlag && this.data.btnFlag == true) {
      wx.redirectTo({
        url: '/pages/filing/family/family?family=family',
      })
    } else {
      wx.navigateTo({
        url: '/pages/filing/family/family'
      })
    }

  },
  dictionary() {
    // 获取字典
    getDictionary().then(res => {
      const sexCondition = this.data.personageSex + this.data.sexValue
      console.log(res.data.data);
      const list = res.data.data.filter(item => {
        if (item.sex == sexCondition) {
          return {
            lables: item.name,
            value: item.id,
            label: item.name + '   ' + '(' + item.left + '----' + item.right + ')',
            left: item.left,
            right: item.right
          }
        }
      })
      console.log(list);
      const lists = list.map(item => {
        return {
          lables: item.name,
          value: item.id,
          label: item.name + '   ' + '(' + item.left + '----' + item.right + ')',
          left: item.left,
          right: item.right
        }
      })
      this.setData({
        list: lists
      })
    })
  },
  nonEmpty() {
    if (this.data.nameValue == '' & this.data.sexValue == '请选择性别' & this.data.name == '请选择你们的关系') {
      Message.error({
        context: this,
        offset: [20, 32],
        duration: 2000,
        content: '输入框不能为空',
      });
      return
    }

  },
  // 获取用户个人信息
  getUserInfo() {
    const userId = GETSTORAGE('userId')
    getPersonInfoApi({
      userId: userId
    }).then(res => {
      console.log(res, "用户个人信息");
      if (this.data.btnFlag) {
        this.setData({
          personageSex: res.data.data.sex
        })
      } else {
        this.setData({
          personageSex: res.data.data.sex,
          userFamilyId: res.data.data.currentFamilyId
        })
      }

    })
  },
  return () {
    wx.navigateBack({
      delta: 1,
    })
  },
  goFamilyRelationship() {
    wx.redirectTo({
      url: '/pages/filing/invite-the-family/invite-the-family?inviteJoin=inviteJoin',
    })
  },
  onLoad(options) {
    console.log(options, '----');
    if (options.family && options.family == 'family') {
      this.setData({
        btnFlag: true,
        userFamilyId: options.familyId
      })
    } else {
      this.setData({
        btnFlag: false
      })
    }
    this.getUserInfo()
  },
  update() {
    const familyId = GETSTORAGE('familyId')
    updateFamilyArchiveStatus({
      familyId: familyId,
      status: "health_manage"
    }).then(res => {})
  },

});